package com.dirven.game.sprite;

import com.dirven.game.Coord;
import com.dirven.game.DirVenGame;
import com.dirven.game.Grid;
import com.dirven.game.geometry.Line;

public class MetalBox extends GridSprite {

	Line top, bottom, right, left;
	
	boolean blockedTop = false, 
			blockedBottom = false, 
			blockedRight = false, 
			blockedLeft = false;
	
	public MetalBox(int x, int y) {
		super(x, y, GridSprite.TYPE_REFLECT, "metalbox", "metalsprite.png");
		top = new Line(x*Grid.cellWidth, (y+1)*Grid.cellHeight, x*Grid.cellWidth, (y+1)*Grid.cellHeight);
		bottom = new Line(x*Grid.cellWidth, y*Grid.cellHeight, (x+1)*Grid.cellWidth, y*Grid.cellHeight);
		left = new Line(x*Grid.cellWidth, y*Grid.cellHeight, x*Grid.cellWidth, (y+1)*Grid.cellHeight);
		right = new Line((x+1)*Grid.cellWidth, y*Grid.cellHeight, (x+1)*Grid.cellWidth, (y+1)*Grid.cellHeight);
		
		
	}
	
	private boolean isReflectiveSpriteAbove() {
		Coord c = super.getHeadPosition();
		
		boolean isAbove = isReflectiveSprite((int)c.x, (int)c.y+1);
		c = null;
		return isAbove;
	}
	
	private boolean isReflectiveSpriteBelow() {
		Coord c = super.getHeadPosition();
		
		boolean isAbove = isReflectiveSprite((int)c.x, (int)c.y-1);
		c = null;
		return isAbove;
	}
	
	private boolean isReflectiveSpriteLeft() {
		Coord c = super.getHeadPosition();
		
		boolean isAbove = isReflectiveSprite((int)c.x-1, (int)c.y);
		c = null;
		return isAbove;
	}
	
	private boolean isReflectiveSpriteRight() {
		Coord c = super.getHeadPosition();
		
		boolean isAbove = isReflectiveSprite((int)c.x+1, (int)c.y);
		c = null;
		return isAbove;
	}
	
	private boolean isReflectiveSprite(int x, int y) {
		boolean reflective = false;
		Coord c = super.getHeadPosition();
		GridSprite gs = Grid.getGridSprite(x, y);
		
		if(gs != null) {
			if(gs.getType() == GridSprite.TYPE_REFLECT) {
				reflective = true;
			}
		}
		gs = null;
		c = null;
		
		return reflective;
	}

	@Override
	public void movementAI() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCollide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onKilled() {
		// TODO Auto-generated method stub

	}
	
	public void onReflect(Laser laser) {
		
		blockedTop = isReflectiveSpriteAbove();
		blockedBottom = isReflectiveSpriteBelow();
		blockedLeft = isReflectiveSpriteLeft();
		blockedRight = isReflectiveSpriteRight();
		
		isReflecting = true;
		
		Coord topPOI = laser.beamLine.getPOI(top);
		Coord bottomPOI = laser.beamLine.getPOI(bottom);
		Coord rightPOI = laser.beamLine.getPOI(right);
		Coord leftPOI = laser.beamLine.getPOI(left);
		
		// In the next section the program tries to find the closest 
		// POI (beam line intersecting the border of the boxes), whether that
		// be the top, bottom, left or right border. The border also can't 
		// be blocked off by another grid metal box.
		
		float topPOIDistance = -1,
			  bottomPOIDistance = -1,
			  leftPOIDistance = -1,
			  rightPOIDistance = -1;
			  
		if(topPOI != null && !blockedTop) {
			topPOIDistance = laser.getDistanceToPoint(topPOI.x, topPOI.y);
		}
		if(bottomPOI != null && !blockedBottom) {
			bottomPOIDistance = laser.getDistanceToPoint(bottomPOI.x, bottomPOI.y);
		}
		if(rightPOI != null && !blockedRight) {
			rightPOIDistance = laser.getDistanceToPoint(rightPOI.x, rightPOI.y);
		}
		if(leftPOI != null && !blockedLeft) {
			leftPOIDistance = laser.getDistanceToPoint(leftPOI.x, leftPOI.y);
		}
		
		
		boolean topReflect = false,
				bottomReflect = false,
				leftReflect = false,
				rightReflect = false;
		
		float closestDistance = DirVenGame.screenWidth * DirVenGame.screenHeight;
		if(!blockedTop && topPOIDistance != -1 && topPOIDistance < closestDistance) {
			closestDistance = topPOIDistance;
			topReflect = true;
		}
		if(!blockedBottom && bottomPOIDistance != -1 && bottomPOIDistance < closestDistance) {
			closestDistance = bottomPOIDistance;
			topReflect = false;
			bottomReflect = true;
			leftReflect = false;
			rightReflect = false;
		}
		if(!blockedRight && rightPOIDistance != -1 && rightPOIDistance < closestDistance) {
			closestDistance = rightPOIDistance;
			topReflect = false;
			bottomReflect = false;
			leftReflect = false;
			rightReflect = true;
		}
		
		if(!blockedLeft && leftPOIDistance != -1 && leftPOIDistance < closestDistance) {
			closestDistance = leftPOIDistance;
			topReflect = false;
			bottomReflect = false;
			leftReflect = true;
			rightReflect = false;
		}
		
			  
		if(topReflect) {
			System.out.println("laser hit top.");
			laser.setupBeam(topPOI.x, topPOI.y);
			float angle = top.getIntersectingAngle(laser.beamLine);
			System.out.println("angle: "+angle);
			laser.setReflection(topPOI.x, topPOI.y, 90-180-angle);
			
		}
		
		if(bottomReflect) {
			System.out.println("laser hit bottom.");
			laser.setupBeam(bottomPOI.x, bottomPOI.y);
		}
		
		if(rightReflect) {
			System.out.println("laser hit right.");
			laser.setupBeam(rightPOI.x, rightPOI.y);
			
			float angle = right.getIntersectingAngle(laser.beamLine);
			System.out.println("angle: "+angle);
			laser.setReflection(rightPOI.x, rightPOI.y, -(90-angle));
			
		}
		
		if(leftReflect) {
			System.out.println("laser hit left.");
			laser.setupBeam(leftPOI.x, leftPOI.y);
			
			float angle = left.getIntersectingAngle(laser.beamLine);
			System.out.println("angle: "+angle);
			laser.setReflection(leftPOI.x, leftPOI.y, (90-angle));
			
		}
	}

}
